package com.shop.clientorder.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.clientorder.domain.FullAdders;
import com.shop.clientorder.domain.OrderItemVo;
import com.shop.clientorder.feign.StorageRemote;
import com.shop.clientorder.utils.DelayedQueueUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import com.shop.clientorder.mapper.OmsOrderItemMapper;
import com.shop.clientorder.domain.OmsOrderItem;
import com.shop.clientorder.service.IOmsOrderItemService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 订单项信息Service业务层处理
 *
 * @author czb
 * @date 2024-06-25
 */
@Service
public class OmsOrderItemServiceImpl implements IOmsOrderItemService
{
    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
//    @Autowired
//    private KafkaTemplate kafkaTemplate;
    @Autowired
    private StorageRemote storageRemote;
    @Autowired
    private DelayedQueueUtil delayedQueueUtil;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 查询订单项信息
     *
     * @param id 订单项信息主键
     * @return 订单项信息
     */
    @Override
    public OmsOrderItem selectOmsOrderItemById(Long id)
    {
        return omsOrderItemMapper.selectOmsOrderItemById(id);
    }

    /**
     * 查询订单项信息列表
     *
     * @param orderSn 订单项信息
     * @return 订单项信息
     */

    /**
     * 新增订单项信息
     *
     * @param omsOrderItem 订单项信息
     * @return 结果
     */
    @Override
    public int insertOmsOrderItem(OmsOrderItem omsOrderItem)
    {

        return omsOrderItemMapper.insertOmsOrderItem(omsOrderItem);
    }

    /**
     * 修改订单项信息
     *
     * @param omsOrderItem 订单项信息
     * @return 结果
     */
    @Override
    public int updateOmsOrderItem(OmsOrderItem omsOrderItem)
    {
        return omsOrderItemMapper.updateOmsOrderItem(omsOrderItem);
    }

    /**
     * 批量删除订单项信息
     *
     * @param ids 需要删除的订单项信息主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrderItemByIds(Long[] ids)
    {
        return omsOrderItemMapper.deleteOmsOrderItemByIds(ids);
    }

    /**
     * 删除订单项信息信息
     *
     * @param id 订单项信息主键
     * @return 结果
     */
    @Override
    public int deleteOmsOrderItemById(Long id)
    {
        return omsOrderItemMapper.deleteOmsOrderItemById(id);
    }

    @Override
    public List<OmsOrderItem> selectOmsOrderItemList(String orderSn) {
        return omsOrderItemMapper.selectOmsOrderItemList(orderSn);
    }

    @Override
//    @Transactional(rollbackFor = Exception.class)
    @Transactional
    public int settlementOmsOrderItem(OrderItemVo orderItemVo) {
        String jsonString = JSONObject.toJSONString(orderItemVo);
//        kafkaTemplate.send("SEND_ADD_ORDER", UUID.randomUUID().toString(), orderItemVo);
        rabbitTemplate.convertAndSend("ORDER_ADD",jsonString,message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
        return 0;
    }

    @Override
    public FullAdders FindAddrByUserId(Long userId) {

        return omsOrderItemMapper.FindAddrByUserId(userId);
    }

    @Override
    public void addOrderItem(OmsOrderItem omsOrderItem) {
        omsOrderItemMapper.addOrderItem(omsOrderItem);
    }
}